﻿#region Vương văn hùng
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using System.ServiceModel;
using System.Runtime.Serialization;
using System.ServiceModel.Description;
using Interface;
using BusinessLogic;

namespace Service
{
    public class Processing
    {
        private string link;
        public string Link
        {
            get { return link; }
            set { link = value; }
        }
        private string iP;
        public string IP
        {
            get { return iP; }
            set { iP = value; }
        }
        private int port;
        public int Port
        {
            get { return port; }
            set { port = value; }
        }
        private string names;
        public string Names
        {
            get { return names; }
            set { names = value; }
        }
        private ServiceHost host;                       
        public ServiceHost Host
        {
            get { return host; }
            set { host = value; }
        }
        public Boolean Start()
        {
            Boolean kt = false;
            try
            {
                host = new ServiceHost(typeof(BusinessLogic.DataObject));
                BasicHttpBinding bind = new BasicHttpBinding();
                bind.MaxBufferPoolSize = (int)67108864;
                bind.MaxBufferSize = 67108864;
                bind.MaxReceivedMessageSize = (int)67108864;
                bind.TransferMode = TransferMode.Buffered;
                bind.ReaderQuotas.MaxArrayLength = 67108864;
                bind.ReaderQuotas.MaxBytesPerRead = 67108864;
                bind.ReaderQuotas.MaxStringContentLength = 67108864;
                bind.Security.Mode = BasicHttpSecurityMode.None;
                bind.HostNameComparisonMode = HostNameComparisonMode.Exact;
                ServiceThrottlingBehavior throttle;
                throttle = host.Description.Behaviors.Find<ServiceThrottlingBehavior>();
                if (throttle == null)
                {
                    throttle = new ServiceThrottlingBehavior();
                    throttle.MaxConcurrentCalls = 100;
                    throttle.MaxConcurrentSessions = 100;
                    host.Description.Behaviors.Add(throttle);
                }
                bind.ReceiveTimeout = new TimeSpan(20, 0, 0);
                this.link = "http://" + this.iP + ":" + (this.port) + "/" + this.names + "/";
                host.AddServiceEndpoint(typeof(Interface.IDataObject), bind,link);
                try
                {
                    host.Open();
                    kt = true;
                }
                catch (Exception ex)
                {
                    string s = ex.Message.ToString();
                    kt = false;
                }
                finally
                {
                    if (host.State == CommunicationState.Opened)
                    { kt = true; }
                    else
                    { kt = false; }
                }
            }
            catch (Exception)
            { kt = false; }
            return kt;
        }

        public Boolean Stop()
        {
            Boolean kt = false;
            try
            {
                if (host == null)
                { return true; }
                host.Close();
                kt = true;
            }
            catch (Exception)
            {
                kt = false;
            }
            return kt;
        }
    }
}
#endregion
